package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.constant.MqConstants;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.TemperatureZone;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.TemperAreaConfig;
import com.lvy.hczn.transmit.mapper.TemperAreaConfigMapper;
import com.lvy.hczn.transmit.service.TemperAreaConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
@Slf4j
public class TemperAreaConfigServiceImpl extends ServiceImpl<TemperAreaConfigMapper, TemperAreaConfig> implements TemperAreaConfigService {

    @Autowired
    private TemperAreaConfigMapper temperAreaConfigMapper;

    @Autowired
    private ProducerUtil producerUtil;

    /**
     * 查询温区配置
     *
     * @param temperAreaId 温区配置主键
     * @return 温区配置
     */
    @Override
    public TemperAreaConfig selectTemperAreaConfigByTemperAreaId(String temperAreaId) {
        return temperAreaConfigMapper.selectById(temperAreaId);
    }

    /**
     * 查询温区配置列表
     *
     * @param temperAreaConfig 温区配置
     * @return 温区配置
     */
    @Override
    public List<TemperAreaConfig> selectTemperAreaConfigList(TemperAreaConfig temperAreaConfig) {
        return temperAreaConfigMapper.selectTemperAreaConfigList(temperAreaConfig);
    }

    /**
     * 新增温区配置
     *
     * @param temperAreaConfig 温区配置
     * @return 结果
     */
    @Override
    public int insertTemperAreaConfig(TemperAreaConfig temperAreaConfig) {
        return temperAreaConfigMapper.insert(temperAreaConfig);
    }

    /**
     * 修改温区配置
     *
     * @param temperAreaConfig 温区配置
     * @return 结果
     */
    @Override
    public int updateTemperAreaConfig(TemperAreaConfig temperAreaConfig) throws Exception {
        if (!StrUtil.isEmpty(temperAreaConfig.getTemperAreaId())) {
            addData(temperAreaConfig);
        }
        return 1;
    }

    /**
     * 批量删除温区配置
     *
     * @param temperAreaIds 需要删除的温区配置主键
     * @return 结果
     */
    @Override
    public int deleteTemperAreaConfigByTemperAreaIds(String temperAreaIds) {
        String[] arrId = Convert.toStrArray(temperAreaIds);
        if (arrId != null) {
            for (String id : arrId) {
                super.removeById(id);
            }
            return arrId.length;
        } else {
            return 0;
        }
    }

    /**
     * 删除温区配置信息
     *
     * @param temperAreaId 温区配置主键
     * @return 结果
     */
    @Override
    public int deleteTemperAreaConfigByTemperAreaId(String temperAreaId) {
        boolean flag = super.removeById(temperAreaId);
        if (flag) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 平台温区配置表同步
     *
     * @param temperAreaConfig
     * @return
     */
    @Override
    public int addData(TemperAreaConfig temperAreaConfig) throws Exception {

        if (temperAreaConfig == null) {
            throw new UtilException("数据为空");
        }
        log.error("addData:{}", JSONUtil.toJsonStr(temperAreaConfig));
        if (StrUtil.hasEmpty(temperAreaConfig.getTemperAreaId(), temperAreaConfig.getTemperAreaName())) {
            throw new UtilException("编码或名称为空");
        }

        /**数据转换**/
        ParamInfo<TemperatureZone> paramInfo = new ParamInfo<>();
        TemperatureZone temperatureZone = new TemperatureZone();
//        temperatureZone.setTemperatureZoneCode(temperAreaConfig.getTemperAreaId());
        temperatureZone.setName(temperAreaConfig.getTemperAreaName());
        temperatureZone.setMaxTemperature(temperAreaConfig.getLimitHigh());
        temperatureZone.setLowTemperature(temperAreaConfig.getLimitLow());
        if (temperAreaConfig.getTemperatureZoneCode() != null) {
            temperatureZone.setTemperatureZoneCode(String.valueOf(temperAreaConfig.getTemperatureZoneCode()));
        }

        if (temperAreaConfig.getStatus() != null && temperAreaConfig.getStatus() < 0) {
            temperatureZone.setDelFlag(Integer.valueOf(Constants.DEL_FLAG_DELETE_TRUE));
        } else {
            temperatureZone.setDelFlag(Integer.valueOf(Constants.DEL_FLAG_DELETE_FALSE));
        }
        temperatureZone.setStatus(temperAreaConfig.getStatus());

        temperatureZone.setTemperAreaVolume(BigDecimal.ZERO);

        temperAreaConfig.setId(temperAreaConfig.getTemperAreaId());
        boolean flag = super.saveOrUpdate(temperAreaConfig);

        temperatureZone.setId(temperAreaConfig.getTemperAreaId());
        paramInfo.setData(temperatureZone);
        if (flag) {
            producerUtil.sendAsyncMsg(MqConstants.TOPIC_TEMPER_AREA_CONFIG, null, JSONUtil.toJsonStr(paramInfo).getBytes(StandardCharsets.UTF_8), IdUtil.fastSimpleUUID());
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 同步温区配置表接口
     *
     * @return
     */
    @Override
    public List<TemperatureZone> syncTemperArea() {
        return temperAreaConfigMapper.syncTemperArea();
    }
}
